<template>
  <div class="app-container">
    <el-card shadow="always">
      <el-alert
        title="系统基本信息设置"
        type="success"
        :closable="false"
      />
      <el-form ref="form" :rules="rules" :model="form" label-width="120px" style="margin-top: 20px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="网站名称" prop="title">
              <el-input v-model="form.title" placeholder="请输入网站名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学校名称" prop="schoolName">
              <el-input v-model="form.schoolName" placeholder="请输入学校名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="网站备案号" prop="caseNumber">
              <el-input v-model="form.caseNumber" placeholder="请输入网站备案号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系方式" prop="schoolPhone">
              <el-input v-model="form.schoolPhone" placeholder="请输入联系方式" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="学校地址" prop="schoolAddress">
              <el-input v-model="form.schoolAddress" placeholder="请输入学校地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学校logo" prop="schoolLogo">
              <el-upload
                class="avatar-uploader"
                :action="baseURL+'rest/api/upload/sfile'"
                :headers="customHeader"
                :show-file-list="false"
                accept=".png"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="form.schoolLogo" :src="form.schoolLogo" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon" />
              </el-upload>
              <div slot="tip" class="el-upload__tip">.png 图片</div>
              <el-input v-model="form.schoolLogo" type="hidden" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="菜单栏logo" prop="schoolLogoTop">
              <el-upload
                class="avatar-uploader"
                :action="baseURL+'rest/api/upload/sfile'"
                :headers="customHeader"
                :show-file-list="false"
                accept=".png"
                :on-success="handleLogoTopSuccess"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="form.schoolLogoTop" :src="form.schoolLogoTop" class="logoTop">
                <i v-else class="el-icon-plus avatar-uploader-icon" />
              </el-upload>
              <div slot="tip" class="el-upload__tip">.png 图片</div>
              <el-input v-model="form.schoolLogoTop" type="hidden" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="页脚logo" prop="schoolLogoBottom">
              <el-upload
                class="avatar-uploader"
                :action="baseURL+'rest/api/upload/sfile'"
                :headers="customHeader"
                :show-file-list="false"
                accept=".png"
                :on-success="handleLogoBottomSuccess"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="form.schoolLogoBottom" :src="form.schoolLogoBottom" class="logoTop">
                <i v-else class="el-icon-plus avatar-uploader-icon" />
              </el-upload>
              <div slot="tip" class="el-upload__tip">.png 图片</div>
              <el-input v-model="form.schoolLogoBottom" type="hidden" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="页脚背景图" prop="schoolBottomImage">
              <div class="schoolBottomImage" @click="schoolBottomImage">
                <img v-if="form.schoolBottomImage" :src="form.schoolBottomImage" class="school-bak-image">
                <i v-else class="el-icon-plus avatar-uploader-icon" />
              </div>
              <el-input v-model="form.schoolBottomImage" type="hidden" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="网站背景图" prop="backgroundImage">
              <div class="schoolBottomImage" @click="schoolBackgroundImage">
                <img v-if="form.backgroundImage" :src="form.backgroundImage" class="school-bak-image">
                <i v-else class="el-icon-plus avatar-uploader-icon" />
              </div>
              <el-input v-model="form.backgroundImage" type="hidden" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="统计背景" prop="countImage">
              <div class="schoolBottomImage" @click="schoolCountImage">
                <img v-if="form.countImage" :src="form.countImage" class="school-bak-image">
                <i v-else class="el-icon-plus avatar-uploader-icon" />
              </div>
              <el-input v-model="form.countImage" type="hidden" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item>
              <el-button type="primary" @click="onSubmit('form')">立即创建</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <!--  页脚背景图片剪切  -->
    <el-dialog :visible.sync="dialogTableVisible" width="70%">
      <cropper-image
        ref="child"
        :name="cropperName"
        :auto-crop-height="autoCropHeight"
        :auto-crop-width="autoCropWidth"
        :fixed-number="fixedNumber"
        @uploadImgSuccess="handleUploadSuccess"
      />
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'
import CropperImage from '@/common/cropper/index'
import { saveConfig, queryEntity } from '@/api/config'
export default {
  name: 'SystemSettingIndex',
  components: { CropperImage },
  data() {
    return {
      baseURL: process.env.NODE_ENV === 'production' ? process.env.VUE_APP_BASE_API : '/', // api 的 base_url
      customHeader: { 'APEXEDU': getToken() },
      cropperName: 'uploadImgSuccess',
      fixedNumber: [1, 1],
      autoCropHeight: 50,
      autoCropWidth: 50,
      dialogTableVisible: false,
      dialogBackgroundVisible: false,
      form: {
        title: '',
        schoolName: '',
        caseNumber: '',
        schoolPhone: '',
        schoolAddress: '',
        schoolLogo: '',
        schoolLogoTop: '',
        schoolLogoBottom: '',
        schoolBottomImage: '',
        backgroundImage: '',
        countImage: ''
      },
      rules: {
        title: [{ required: true, message: '请输入网站名称', trigger: 'blur' }],
        schoolName: [{ required: true, message: '请输入学校名称', trigger: 'blur' }],
        caseNumber: [{ required: true, message: '请输入网站备案号', trigger: 'blur' }],
        schoolPhone: [{ required: true, message: '请输入联系方式', trigger: 'blur' }],
        schoolAddress: [{ required: true, message: '请输入学校地址', trigger: 'blur' }],
        schoolLogo: [{ required: true, message: '学校logo不能为空', trigger: 'blur' }],
        schoolLogoTop: [{ required: true, message: '学校菜单Logo不能为空', trigger: 'blur' }],
        schoolLogoBottom: [{ required: true, message: '学校页脚Logo不能为空', trigger: 'blur' }],
        schoolBottomImage: [{ required: true, message: '页脚背景图不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.init()
  },
  methods: {
    handleAvatarSuccess(res, file) {
      const { url } = res
      this.form.schoolLogo = url
    },
    handleLogoTopSuccess(res, file) {
      const { url } = res
      this.form.schoolLogoTop = url
    },
    handleLogoBottomSuccess(res, file) {
      const { url } = res
      this.form.schoolLogoBottom = url
    },
    handleSchoolBottomSuccess(res, file) {
      const { url } = res
      this.form.schoolBottomImage = url
    },
    handleUploadSuccess(data) {
      if (this.cropperName === 'uploadImgSuccess') {
        this.form.schoolBottomImage = data.url
        this.dialogTableVisible = false
      }
      if (this.cropperName === 'handleUploadBackgroundSuccess') {
        this.form.backgroundImage = data.url
        this.dialogTableVisible = false
      }
      if (this.cropperName === 'countImage') {
        this.form.countImage = data.url
        this.dialogTableVisible = false
      }
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isLt2M) {
        this.$message.error('上传大小不能超过 2MB!')
      }
      return isLt2M
    },
    schoolBottomImage() {
      this.cropperName = 'uploadImgSuccess'
      this.fixedNumber = [5.2, 1]
      this.autoCropWidth = 230
      this.dialogTableVisible = true
    },
    schoolBackgroundImage() {
      this.cropperName = 'handleUploadBackgroundSuccess'
      this.fixedNumber = [1, 1]
      this.autoCropWidth = 50
      this.autoCropHeight = 50
      this.dialogTableVisible = true
    },
    schoolCountImage() {
      this.cropperName = 'countImage'
      this.fixedNumber = [2.1, 1]
      this.autoCropWidth = 250
      this.dialogTableVisible = true
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          saveConfig(this.form).then((res) => {
            this.$message.success('保存成功！')
          }).catch(() => {
            this.$message.error('保存失败！')
          })
        } else {
          return false
        }
      })
    },
    async init() {
      queryEntity().then((res) => {
        const { data } = res
        if (data !== null && undefined !== data) {
          this.form = data
        }
      })
    }
  }
}
</script>

<style scoped>
.avatar-uploader {
  width: 40px;
  height: 40px;
  border: 1px dashed #9b9b9b;
  border-radius: 2px;
  text-align: center;
  line-height: 40px;
}
.schoolBottomImage {
  width: 100%;
  height: 250px;
  border: 1px dashed #9b9b9b;
  border-radius: 4px;
  text-align: center;
  line-height: 250px;
  font-size: 34px;
}
.school-bak-image {
  width: 100%;
  height: 250px;
}
.avatar {
  width: 40px;
  height: 40px;
}
.logoTop {
  height: 40px;
}

</style>
